import { SET_CURRENT_CATEGORY } from "../actiontypes";
import { APIgetSliders } from "@/api/home";
import * as TYPES from "../actiontypes";
import { APIgetLessons } from "@/api/home";
import { StoreGetState, StoreDispatch } from "@/store/store";
const homeDispatchAction = {
	setCurrentCategory(curentCategory: string) {
		return {
			type: SET_CURRENT_CATEGORY,
			payload: curentCategory,
		};
	},
	getSliders() {
		return {
			type: TYPES.GET_SLIDERS,
			payload: APIgetSliders(),
		};
	},
	getLessons() {
		return (dispatch: StoreDispatch, getState: StoreGetState) => {
			(async function () {
				let {
					currentCategory,
					lessons: { hasMore, offset, limit, loading },
				} = getState().home;
				if (hasMore && !loading) {
					dispatch({ type: TYPES.SET_LESSONS_LOADING, payload: true });
					let result = await APIgetLessons(currentCategory, offset, limit);
					dispatch({ type: TYPES.SET_LESSONS, payload: result.data });
				}
			})();
		};
	},
	refreshLessons() {
		return (dispatch: StoreDispatch, getState: StoreGetState) => {
			(async function () {
				let {
					currentCategory,
					lessons: { limit, loading },
				} = getState().home;
				if (!loading) {
					dispatch({ type: TYPES.SET_LESSONS_LOADING, payload: true });
					let result = await APIgetLessons(currentCategory, 0, limit);
					dispatch({ type: TYPES.REFRESH_LESSONS, payload: result.data });
				}
			})();
		};
	},
};
export default homeDispatchAction;
